<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="停车场名" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入停车场名" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="状态" prop="state">
        <el-select v-model="queryParams.state" placeholder="请选择状态" clearable>
          <el-option v-for="dict in dict.type.park_status" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['msr:park:add']">新增</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['msr:park:edit']">修改</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['msr:park:remove']">删除</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['msr:park:export']">导出</el-button>
      </el-col> -->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="parkList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" align="center" prop="id" />
      <el-table-column label="停车场名" align="center" prop="name" />
      <el-table-column label="车位数" align="center" prop="number" />
      <el-table-column label="停车场地址" align="center" prop="address" />
      <el-table-column label="手机" align="center" prop="phone" />
      <el-table-column label="状态" align="center" prop="state">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.park_status" :value="scope.row.state" />
          <!-- <el-button v-if="scope.row.state == 0" size="mini" style="background-color: blue; color: white">正常</el-button>
          <el-button v-else-if="scope.row.state == 1" size="mini"
            style="background-color: blue; color: white">车位已满</el-button>
          <el-button v-else-if="scope.row.state == 2" size="mini"
            style="background-color: blue; color: white">暂停营业</el-button> -->
        </template>
      </el-table-column>
      <el-table-column label="已用车位数" align="center" prop="useNumber" />
      <el-table-column label="计费规则" align="center" prop="ruleName">
        <!-- <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_user_sex" :value="scope.row.ruleId" />
        </template> -->
      </el-table-column>
      <el-table-column label="收费二维码" align="center" prop="image" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.image" :width="50" :height="50" />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="150">
        <template slot-scope="scope">

          <!-- <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['msr:park:remove']">删除</el-button> -->
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleDownload(scope.row)">下载二维码</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['msr:park:edit']">修改</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改停车场管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="停车场名" prop="name">
          <el-input v-model="form.name" placeholder="请输入停车场名" />
        </el-form-item>
        <el-form-item label="车位数" prop="number">
          <el-input v-model="form.number" placeholder="请输入车位数" />
        </el-form-item>
        <el-form-item label="停车场地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入停车场地址" />
        </el-form-item>
        <el-form-item label="手机" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入手机" />
        </el-form-item>
        <el-form-item label="状态" prop="state">
          <el-select v-model="form.state" placeholder="请选择状态">
            <el-option v-for="dict in dict.type.park_status" :key="dict.value" :label="dict.label"
              :value="parseInt(dict.value)"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="已用车位数" prop="useNumber">
          <el-input v-model="form.useNumber" placeholder="请输入已用车位数" />
        </el-form-item>
        <el-form-item label="计费规则" prop="ruleId">
          <el-select v-model="form.ruleId" placeholder="请选择计费规则">
            <el-option v-for="dict in ruleList" :key="dict.id" :label="dict.name"
              :value="parseInt(dict.id)"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="收费二维码" prop="image">
          <image-upload v-model="form.image" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listPark,
  getPark,
  delPark,
  addPark,
  updatePark,
} from "@/api/msr/park";
import { listRule } from "@/api/msr/rule";


export default {
  name: "Park",
  dicts: ["park_status", "sys_user_sex"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 停车场管理表格数据
      parkList: [],
      // 查询规则名
      ruleList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        state: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // 文件下载处理
    handleDownload(row) {
      const resource = row.image;
      // 默认方法
      this.$download.resource(resource);
    },
    /** 查询停车场管理列表 */
    getList() {
      this.loading = true;
      listPark(this.queryParams).then((response) => {
        this.parkList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
      //查询所有的停车场规则名
      listRule().then((response) => {
        this.ruleList = response.rows;
        console.log(this.ruleList);
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        number: null,
        address: null,
        phone: null,
        state: null,
        useNumber: null,
        ruleId: null,
        image: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加停车场管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getPark(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改停车场管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updatePark(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPark(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除停车场管理编号为"' + ids + '"的数据项？')
        .then(function () {
          return delPark(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "msr/park/export",
        {
          ...this.queryParams,
        },
        `park_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
